﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TMEVAR
    {
        public TMEVAR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VMEVAR();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["MEVMANDANT"] = app.Mandant;
            dwData["MEVAGANG"] = " AND MEVAGANG LIKE '" + ART + "%'";
            dwData["MEVMTYP"] = " AND MEVMTYP LIKE '" + FARBE + "%'";
            SResult = TMV.QMER02(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    return;
                }
                if (EXAUF.Length < 35)
                    throw new Exception("必需有创建指令");
                if (cAM06.TBMain == null)
                    throw new Exception("必需定位设备");
                if (cAM06.TBMain.Rows.Count == 0)
                    throw new Exception("必需定位设备");
                PM["TBMOD"] = cAM06.TBMain;
                PM["AGANG"] = EXAUF.Substring(0, 15);
                PM["MTYP"] = EXAUF.Substring(15, 8);
                PM["VARHNR"] = EXAUF.Substring(31, 2);
                PM["VARUNR"] = EXAUF.Substring(33, 2);
                TMV.NEWME05(PM);

                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DMER01(dwMaster);
            filter = "MEVAGANG='" + dwMaster["MEVAGANG"].ToString() + "' AND MEVMTYP='" + dwMaster["MEVMTYP"].ToString() + "' AND MEVMNRKRS='" + dwMaster["MEVMNRKRS"].ToString()
                + "' AND MEVMLFDNR='" + dwMaster["MEVMLFDNR"].ToString()
                + "' AND MEVMPOS='" + dwMaster["MEVMPOS"].ToString()
                + "' AND MEVEVARHNR='" + dwMaster["MEVEVARHNR"].ToString()
                + "' AND MEVEVARUNR='" + dwMaster["MEVEVARUNR"].ToString()
                + "' AND MEVMODNR='" + dwMaster["MEVMODNR"].ToString()
                + "' AND MEVEINSTNR='" + dwMaster["MEVEINSTNR"].ToString()
                + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                row["MEVWERTN"] = Helpers.Helper.Right("0000000000000000" + row["MEVWERTN"].ToString(), 16);
                row["MEVWERTX"] = (double.Parse(row["MEVWERTN"].ToString()) * 0.000001).ToString().PadRight(16);
                TMV.UpdateMEVAR(row, 0);
            }
           
            base.SaveCommand(sender, e);
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["MEVWERTN"] = Helpers.Helper.Right("0000000000000000" + row["MEVWERTN"].ToString(), 16);
                row["MEVWERTX"] = (double.Parse(row["MEVWERTN"].ToString()) * 0.000001).ToString().PadRight(16);
                TMV.UpdateMEVAR(row, 0);
            }
            base.OnSave();
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length < 41)
                return;
            dwData = TMV.TBMain.NewRow();
            dwData["MEVMANDANT"] = app.Mandant;
            dwData["MEVAGANG"] = " AND MEVAGANG='" + ORDERNUM.Substring(0, 15) + "'";
            dwData["MEVMTYP"] = " AND MEVMTYP='" + ORDERNUM.Substring(15, 8) + "'";
            dwData["MEVMNRKRS"] = " AND MEVMNRKRS='" + ORDERNUM.Substring(23, 2) + "'";
            dwData["MEVMLFDNR"] = " AND MEVMLFDNR='" + ORDERNUM.Substring(25, 4) + "'";
            dwData["MEVMPOS"] = " AND MEVMPOS='" + ORDERNUM.Substring(29, 2) + "'";
            dwData["MEVEVARHNR"] = " AND MEVEVARHNR='" + ORDERNUM.Substring(31, 2) + "'";
            dwData["MEVEVARUNR"] = " AND MEVEVARUNR='" + ORDERNUM.Substring(33, 2) + "'";
            dwData["MEVMODNR"] = " AND MEVMODNR='" + ORDERNUM.Substring(35, 3) + "'";
            dwData["MEVEINSTNR"] = " AND MEVEINSTNR='" + ORDERNUM.Substring(38, 3) + "'";
            SResult = TMV.QMER02(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 35)
                return; 
            dwData = TMV.TBMain.NewRow();
            dwData["MEVMANDANT"] = app.Mandant;
            dwData["MEVAGANG"] = " AND MEVAGANG='" + EXAUF.Substring(0, 15) + "'";
            dwData["MEVMTYP"] = " AND MEVMTYP='" + EXAUF.Substring(15, 8) + "'";
            dwData["MEVMNRKRS"] = " AND MEVMNRKRS='" + EXAUF.Substring(23, 2) + "'";
            dwData["MEVMLFDNR"] = " AND MEVMLFDNR='" + EXAUF.Substring(25, 4) + "'";
            dwData["MEVMPOS"] = " AND MEVMPOS='" + EXAUF.Substring(29, 2) + "'";
            dwData["MEVEVARHNR"] = " AND MEVEVARHNR='" + EXAUF.Substring(31, 2) + "'";
            dwData["MEVEVARUNR"] = " AND MEVEVARUNR='" + EXAUF.Substring(33, 2) + "'";
            SResult = TMV.QMER02(dwData);
            MergeData(SResult, false);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            PM["TBMOD"] = cAM06.TBMain;
            PM["KRS"] = EXAUF.Substring(23, 2);
            PM["LFD"] = EXAUF.Substring(25, 4);
            PM["POS"] = EXAUF.Substring(29, 2);
            PM["AGANG"] = EXAUF.Substring(0, 15);
            PM["MTYP"] = EXAUF.Substring(15, 8);
            PM["VARHNR"] = EXAUF.Substring(31, 2);
            PM["VARUNR"] = EXAUF.Substring(33, 2);
            TMV.NEWME05(PM);
            base.OnEXAUFChg(vlaue);
        } 
    }
}
